﻿Imports DevExpress.XtraEditors

Public Class frmDanhMuc_CapNhat
    Dim Me_ThemSua As String

    Private Sub frmDanhMuc_CapNhat_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.txtTen.Focus()
    End Sub

    Private Sub frmDanhMuc_CapNhat_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitControl()
        InitLookup()

        If frmDanhMuc_CapNhat_AddEdit = "add" Then
            AddNew()
        ElseIf frmDanhMuc_CapNhat_AddEdit = "edit" Then
            LoadEdit()
        End If

    End Sub

    Private Sub InitControl()
        Me.txtDgVl.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        Me.txtDgVl.Properties.DisplayFormat.FormatString = "### ### ###;Null"

        Me.txtDgNc.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        Me.txtDgNc.Properties.DisplayFormat.FormatString = "### ### ###;Null"

        Me.txtDgMtc.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        Me.txtDgMtc.Properties.DisplayFormat.FormatString = "### ### ###;Null"

    End Sub

    Private Sub InitLookup()
        Dim myDtb As New DataTable

        'hang muc
        Me.lueMaHangMuc.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("mahangmuc", "Mã", 20, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("tenhangmuc", "Tên hạng mục", 70, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("hangmuc", "Hạng mục", 90, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Center)})
        Me.lueMaHangMuc.Properties.DisplayMember = "hangmuc"
        Me.lueMaHangMuc.Properties.ValueMember = "mahangmuc"

        Dim myHangMuc As New HangMucF
        myDtb = myHangMuc.GetAll
        Me.lueMaHangMuc.Properties.DataSource = myDtb

    End Sub

    Private Sub AddNew()
        Status(1)

        Me.txtMa.EditValue = -1
        Me.txtTen.EditValue = ""
        Me.txtDvt.EditValue = ""
        Me.txtDgVl.EditValue = 0
        Me.txtDgNc.EditValue = 0
        Me.txtDgMtc.EditValue = 0
        Me.chkHetSuDung.EditValue = False

        Me.lueMaHangMuc.EditValue = ""

        Me.txtTen.Focus()

    End Sub

    Private Sub LoadEdit()
        Status(2)

        Dim myDanhMucF As New DanhMucF
        Dim myDtb As New DataTable
        myDtb = myDanhMucF.GetWhereMa(frmDanhMuc_CapNhat_Ma)
        If myDtb.Rows.Count = 1 Then
            Me.txtMa.EditValue = myDtb.Rows(0).Item("ma")
            Me.txtTen.EditValue = myDtb.Rows(0).Item("ten")
            Me.txtDvt.EditValue = myDtb.Rows(0).Item("dvt")
            Me.txtDgVl.EditValue = myDtb.Rows(0).Item("dgvl")
            Me.txtDgNc.EditValue = myDtb.Rows(0).Item("dgnc")
            Me.txtDgMtc.EditValue = myDtb.Rows(0).Item("dgmtc")
            Me.lueMaHangMuc.EditValue = myDtb.Rows(0).Item("mahangmuc")
            Me.chkHetSuDung.EditValue = Not myDtb.Rows(0).Item("trangthai")
            Me.txtTen.Focus()
        End If
    End Sub

    Private Sub Status(ByVal i As Integer)
        Select Case i
            Case 0
                Me.Text = "Thông tin vật tư"

                Me.txtTen.Enabled = False
                Me.txtDvt.Enabled = False
                Me.txtDgVl.Enabled = False
                Me.txtDgNc.Enabled = False
                Me.txtDgMtc.Enabled = False
                Me.lueMaHangMuc.Enabled = False
              
                Me.cmdThem.Text = "&Thêm mới"
                Me.cmdSua.Text = "&Sửa"

                Me.cmdXoa.Enabled = True

            Case 1
                Me.Text = "Thêm mới vật tư"

                Me.txtTen.Enabled = True
                Me.txtDvt.Enabled = True

                If Project_UserType = "1" Then
                    Me.txtDgVl.Enabled = True
                    Me.txtDgNc.Enabled = True
                    Me.txtDgMtc.Enabled = True

                ElseIf Project_UserType = "3" Then
                    Me.txtDgVl.Enabled = False
                    Me.txtDgNc.Enabled = False
                    Me.txtDgMtc.Enabled = False

                End If
                
                Me.lueMaHangMuc.Enabled = True
                Me.chkHetSuDung.Enabled = False

                Me.cmdThem.Text = "&Ghi"
                Me.cmdSua.Text = "&Bỏ qua"

                Me.cmdXoa.Enabled = False

            Case 2
                Me.Text = "Sửa vật tư"

                Me.txtTen.Enabled = True
                Me.txtDvt.Enabled = True

                Select Case Project_UserType
                    Case "0"
                        Me.txtDgVl.Enabled = True
                        Me.txtDgNc.Enabled = True
                        Me.txtDgMtc.Enabled = True

                    Case "1"
                        Me.txtDgVl.Enabled = True
                        Me.txtDgNc.Enabled = True
                        Me.txtDgMtc.Enabled = True

                    Case "2"
                        Me.txtDgVl.Enabled = False
                        Me.txtDgNc.Enabled = False
                        Me.txtDgMtc.Enabled = False

                    Case "3"
                        Me.txtDgVl.Enabled = False
                        Me.txtDgNc.Enabled = False
                        Me.txtDgMtc.Enabled = False

                End Select

                Me.lueMaHangMuc.Enabled = True
                Me.lueMaHangMuc.Enabled = True

                Me.cmdThem.Text = "&Ghi"
                Me.cmdSua.Text = "&Bỏ qua"

                Me.cmdXoa.Enabled = False

        End Select
    End Sub

    Private Sub cmdThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThem.Click
        If Me.cmdThem.Text = "&Thêm mới" Then
            AddNew()

        ElseIf Me.cmdThem.Text = "&Ghi" Then

            If Not CheckInput() Then Exit Sub
            If Not Save() Then Exit Sub

            XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
            Status(0)
        End If
    End Sub

    Private Function CheckInput() As Boolean
        'ten
        If Me.txtTen.EditValue.ToString.Trim = "" Then
            XtraMessageBox.Show("Tên danh mục không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtTen.Focus()
            Return False
            Exit Function
        End If

        'dvt
        If Me.txtDvt.EditValue.ToString.Trim = "" Then
            XtraMessageBox.Show("Đơn vị tính không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtDvt.Focus()
            Return False
            Exit Function
        End If

        'mahm
        If Me.lueMaHangMuc.EditValue = "" Then
            XtraMessageBox.Show("Mã hạng mục không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.lueMaHangMuc.Focus()
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function Save() As Boolean
        Dim myDanhMuc As New DanhMuc
        Dim myDanhMucF As New DanhMucF

        myDanhMuc.Ma = Me.txtMa.EditValue
        myDanhMuc.Ten = Me.txtTen.EditValue.ToString.Trim
        myDanhMuc.Dvt = Me.txtDvt.EditValue.ToString.Trim

        myDanhMuc.Dgvl = Me.txtDgVl.EditValue
        myDanhMuc.Dgnc = Me.txtDgNc.EditValue
        myDanhMuc.Dgmtc = Me.txtDgMtc.EditValue

        myDanhMuc.MaHangMuc = Me.lueMaHangMuc.EditValue
        myDanhMuc.TrangThai = Not Me.chkHetSuDung.EditValue

        If Not myDanhMucF.Save(myDanhMuc) Then
            XtraMessageBox.Show("Ghi dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Sub cmdSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSua.Click
        If Me.cmdSua.Text = "&Sửa" Then
            Status(2)
        ElseIf Me.cmdSua.Text = "&Bỏ qua" Then
            Status(0)
        End If
    End Sub

    Private Sub cmdThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub cmdXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoa.Click
        Dim myLoaiChietTinhCtF As New LoaiChietTinhCtF
        If myLoaiChietTinhCtF.ExistsMa(Me.txtMa.EditValue) Then
            XtraMessageBox.Show("Danh mục đã tồn tại trong loại chiết tính", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        Dim myChietTinhCtF As New ChietTinhCtF
        If myChietTinhCtF.ExistsMa(Me.txtMa.EditValue) Then
            XtraMessageBox.Show("Danh mục đã tồn tại trong chiết tính", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If XtraMessageBox.Show("Chắc chắn danh mục ?", "Quản lý chiết tính", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim myDanhMucF As New DanhMucF
            If myDanhMucF.Delete(Me.txtMa.EditValue) Then
                XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Else
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

End Class